<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Group Model
 * @description : Database functions related to polls
 * 
 */
class Poll_model extends CI_Model {

  function __construct() {
    parent::__construct();
  }

  function set_poll($poll_message = NULL, $poll_user_id = NULL, $poll_show_users = 1, $poll_add_option = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($poll_message) && !empty($poll_user_id) && is_numeric($poll_user_id)) {
      $query_insr = array(
          'poll_message' => $poll_message,
          'poll_user_id' => $poll_user_id,
          'poll_date' => date("Y-m-d H:i:s"),
          'poll_show_users' => $poll_show_users,
          'poll_add_option' => $poll_add_option,
          'poll_status' => 1
      );
      $query_exec = $this->db->insert('polls', $query_insr);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result']['poll_id'] = $this->db->insert_id();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function set_poll_option($poll_id = NULL, $poll_option_message = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($poll_id) && is_numeric($poll_id) && !empty($poll_option_message) && !empty($user_id) && is_numeric($user_id)) {
      $query_insr = array(
          'poll_option_poll_id' => $poll_id,
          'poll_option_message' => $poll_option_message,
          'poll_option_user_id' => $user_id,
          'poll_option_status' => 1
      );
      $query_exec = $this->db->insert('polls_options', $query_insr);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->insert_id();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function get_polls($limit = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $this->db->where("poll_status = '1'");
    $this->db->select('polls.*, users.*', FALSE);
    $this->db->select('(SELECT count(*) FROM polls_reply WHERE poll_reply_poll_id = polls.poll_id) AS poll_replies_count', FALSE);
    if (!empty($user_id) && is_numeric($user_id)) {
      $this->db->select('(SELECT MAX(poll_reply_date) FROM polls_reply WHERE poll_reply_poll_id = polls.poll_id AND poll_reply_user_id = ' . $user_id . ') AS my_poll_reply_status', FALSE);
    } else {
      $this->db->select('0 AS my_poll_reply_status', FALSE);
    }
    $this->db->join('users', 'user_id = poll_user_id');
    $this->db->order_by('poll_id', 'desc');
    $query_exec = $this->db->get_where('polls', $query_cond, $limit);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function get_my_polls($limit = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    if (!empty($user_id) && is_numeric($user_id)) {
      $this->db->where("poll_user_id = '$user_id' AND (poll_status = '1' OR poll_status = '2')");
    } else {
      $this->db->where("poll_status = '1'");
    }
    $this->db->select('polls.*, users.*', FALSE);
    $this->db->select('(SELECT count(*) FROM polls_reply WHERE poll_reply_poll_id = polls.poll_id) AS poll_replies_count', FALSE);
    if (!empty($user_id) && is_numeric($user_id)) {
      $this->db->select('(SELECT MAX(poll_reply_date) FROM polls_reply WHERE poll_reply_poll_id = polls.poll_id AND poll_reply_user_id = ' . $user_id . ') AS my_poll_reply_status', FALSE);
    } else {
      $this->db->select('0 AS my_poll_reply_status', FALSE);
    }
    $this->db->join('users', 'user_id = poll_user_id');
    $this->db->order_by('poll_id', 'desc');
    $query_exec = $this->db->get_where('polls', $query_cond, $limit);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function get_top_polls($limit = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $this->db->where("poll_status = '1'");
    $this->db->select('polls.*, users.*', FALSE);
    $this->db->select('(SELECT count(*) FROM polls_reply WHERE poll_reply_poll_id = polls.poll_id) AS poll_replies_count', FALSE);
    if (!empty($user_id) && is_numeric($user_id)) {
      $this->db->select('(SELECT MAX(poll_reply_date) FROM polls_reply WHERE poll_reply_poll_id = polls.poll_id AND poll_reply_user_id = ' . $user_id . ') AS my_poll_reply_status', FALSE);
    } else {
      $this->db->select('0 AS my_poll_reply_status', FALSE);
    }
    $this->db->join('users', 'user_id = poll_user_id');
    $this->db->order_by('poll_replies_count', 'desc');
    $query_exec = $this->db->get_where('polls', $query_cond, $limit);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function get_active_polls($limit = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $this->db->where("poll_status = '1'");
    $this->db->select('polls.*, users.*', FALSE);
    $this->db->select('(SELECT count(*) FROM polls_reply WHERE poll_reply_poll_id = polls.poll_id) AS poll_replies_count', FALSE);
    $this->db->select('(SELECT MAX(poll_reply_date) FROM polls_reply WHERE poll_reply_poll_id = polls.poll_id) AS poll_reply_latest', FALSE);

    if (!empty($user_id) && is_numeric($user_id)) {
      $this->db->select('(SELECT MAX(poll_reply_date) FROM polls_reply WHERE poll_reply_poll_id = polls.poll_id AND poll_reply_user_id = ' . $user_id . ') AS my_poll_reply_status', FALSE);
    } else {
      $this->db->select('0 AS my_poll_reply_status', FALSE);
    }

    $this->db->join('users', 'user_id = poll_user_id');
    $this->db->order_by('poll_reply_latest', 'desc');
    $query_exec = $this->db->get_where('polls', $query_cond, $limit);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function get_comp_polls($limit = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $this->db->where("poll_status = '2'");
    $this->db->select('polls.*, users.*', FALSE);
    $this->db->select('(SELECT count(*) FROM polls_reply WHERE poll_reply_poll_id = polls.poll_id) AS poll_replies_count', FALSE);
    $this->db->select('(SELECT MAX(poll_reply_date) FROM polls_reply WHERE poll_reply_poll_id = polls.poll_id) AS poll_reply_latest', FALSE);
    if (!empty($user_id) && is_numeric($user_id)) {
      $this->db->select('(SELECT MAX(poll_reply_date) FROM polls_reply WHERE poll_reply_poll_id = polls.poll_id AND poll_reply_user_id = ' . $user_id . ') AS my_poll_reply_status', FALSE);
    } else {
      $this->db->select('0 AS my_poll_reply_status', FALSE);
    }
    $this->db->join('users', 'user_id = poll_user_id');
    $this->db->order_by('poll_reply_latest', 'desc');
    $query_exec = $this->db->get_where('polls', $query_cond, $limit);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function get_poll_by_id($poll_id = NULL, $poll_status = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($poll_id) && is_numeric($poll_id)) {
      $query_sql = "SELECT polls.*, users.*, 
        (SELECT count(*) FROM polls_reply WHERE poll_reply_poll_id = polls.poll_id) AS poll_replies_count
        FROM (polls)
        JOIN users ON user_id = poll_user_id
        WHERE poll_id = '$poll_id'
        AND user_status = 1 ";
      if (!empty($poll_status) && is_numeric($poll_status)) {
        $query_sql = $query_sql . " AND (poll_status = $poll_status) ";
      } else {
        $query_sql = $query_sql . " AND (poll_status = 1 OR poll_status = 2) ";
      }
      $query_exec = $this->db->query($query_sql);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_polls_count($user_id = NULL, $poll_status = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    if (!empty($user_id) && is_numeric($user_id)) {
      $query_cond = array('poll_user_id' => $user_id, 'poll_status' => $poll_status);
    } else {
      $query_cond = array('poll_status' => $poll_status);
    }
    $query_exec = $this->db->get_where('polls', $query_cond);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  function get_poll_options($poll_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($poll_id) && is_numeric($poll_id)) {
      $query_cond = array();
      $query_cond['poll_option_poll_id'] = $poll_id;
      $query_cond['poll_option_status'] = 1;
      $query_cond['user_status'] = 1;
      $this->db->join('users', 'user_id = poll_option_user_id');
      $query_exec = $this->db->get_where('polls_options', $query_cond);
      if ($query_exec == TRUE) {
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_poll_reply($poll_id = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($poll_id) && is_numeric($poll_id) && !empty($user_id) && is_numeric($user_id)) {
      $query_cond = array();
      $query_cond['poll_reply_poll_id'] = $poll_id;
      $query_cond['poll_reply_user_id'] = $user_id;
      $query_cond['user_status'] = 1;
      $this->db->join('users', 'user_id = poll_reply_user_id');
      $query_exec = $this->db->get_where('polls_reply', $query_cond);
      if ($query_exec == TRUE) {
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function set_poll_reply($poll_id = NULL, $poll_option_id = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($poll_id) && is_numeric($poll_id) && !empty($poll_option_id) && is_numeric($poll_option_id) && !empty($user_id) && is_numeric($user_id)) {
      $query_insr = array(
          'poll_reply_poll_id' => $poll_id,
          'poll_reply_option_id' => $poll_option_id,
          'poll_reply_user_id' => $user_id,
          'poll_reply_date' => date("Y-m-d H:i:s")
      );
      $query_exec = $this->db->insert('polls_reply', $query_insr);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->insert_id();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function get_poll_reply_count($poll_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($poll_id) && is_numeric($poll_id)) {
      $query_cond = array();
      $query_cond['poll_reply_poll_id'] = $poll_id;
      $query_cond['user_status'] = 1;
      $this->db->join('users', 'user_id = poll_reply_user_id');
      $query_exec = $this->db->get_where('polls_reply', $query_cond);
      if ($query_exec == TRUE) {
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_poll_reply_option_count($poll_option_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($poll_option_id) && is_numeric($poll_option_id)) {
      $query_cond = array();
      $query_cond['poll_reply_option_id'] = $poll_option_id;
      $query_cond['user_status'] = 1;
      $this->db->join('users', 'user_id = poll_reply_user_id');
      $query_exec = $this->db->get_where('polls_reply', $query_cond);
      if ($query_exec == TRUE) {
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_poll_reply_users($poll_id = NULL, $poll_option_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($poll_id) && is_numeric($poll_id) && !empty($poll_option_id) && is_numeric($poll_option_id)) {
      $query_cond = array();
      $query_cond['poll_reply_poll_id'] = $poll_id;
      $query_cond['poll_reply_option_id'] = $poll_option_id;
      $query_cond['user_status'] = 1;
      $this->db->join('users', 'user_id = poll_reply_user_id');
      $this->db->order_by('poll_reply_date', 'desc');
      $query_exec = $this->db->get_where('polls_reply', $query_cond);
      if ($query_exec == TRUE) {
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function update_poll_status_by_id($poll_id = NULL, $user_id = NULL, $poll_status = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($poll_id) && is_numeric($poll_id) && !empty($user_id) && is_numeric($user_id)) {
      $query_cond = array();
      $query_cond['poll_id'] = $poll_id;
      $query_cond['poll_user_id'] = $user_id;
      $query_upda = array();
      $query_upda['poll_status'] = $poll_status;
      $query_exec = $this->db->update('polls', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function update_poll_status_by_admin($poll_id = NULL, $poll_status = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($poll_id) && is_numeric($poll_id)) {
      $query_cond = array();
      $query_cond['poll_id'] = $poll_id;
      $query_upda = array();
      $query_upda['poll_status'] = $poll_status;
      $query_exec = $this->db->update('polls', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function get_my_poll_replies($user_id = NULL, $limit = 100) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id)) {
      $query_cond = array();
      $query_cond['poll_reply_user_id'] = $user_id;
      $query_cond['poll_status'] = 1;
      $this->db->join('polls', 'poll_id = poll_reply_poll_id');
      $this->db->order_by('poll_reply_date');
      $query_exec = $this->db->get_where('polls_reply', $query_cond, $limit);
      if ($query_exec == TRUE) {
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function upd_poll_notify_status($poll_id = NULL, $notify_status = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($poll_id) && is_numeric($poll_id)) {
      $query_cond = array();
      $query_cond['poll_id'] = $poll_id;
      $query_upda = array();
      $query_upda['poll_notification'] = $notify_status;
      $query_exec = $this->db->update('polls', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function get_new_options($poll_id = NULL, $last_option_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($poll_id) && is_numeric($poll_id)) {
      $query_cond = array();
      $query_cond['poll_option_poll_id'] = $poll_id;
      $query_cond['poll_option_status'] = 1;
      $query_cond['user_status'] = 1;
      $this->db->join('users', 'user_id = poll_option_user_id');
      $this->db->order_by('poll_option_id', 'desc');
      $this->db->where('poll_option_id >', $last_option_id);
      $query_exec = $this->db->get_where('polls_options', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_new_polls_count() {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $date_dmy = date('Y-m-d');
    $query = "SELECT COUNT(p1.poll_id) AS new_polls_count FROM polls p1 WHERE (p1.poll_status = 1) AND (DATE(p1.poll_date) = '{$date_dmy}') AND ((SELECT COUNT(pr1.poll_reply_id) FROM polls_reply pr1 WHERE p1.poll_id = pr1.poll_reply_poll_id) = 0)";
    $query_exec = $this->db->query($query);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->row_array();
      $result_data['count'] = $query_exec->row()->new_polls_count;
    }
    return $result_data;
  }

}

?>
